<?php
namespace Home\Controller;
use Think\Controller;

Class OrderIncreaseController extends CommonController{

  public function _initialize() {
      parent::_initialize();
      $this->dbname = CONTROLLER_NAME;
  }

  // public function index(){
  //   if(IS_POST){
  //     $this->assign('time',$_REQUEST['time6']);
  //   }else{
  //     $this->assign('time',time());
  //   }
  //     $this->huikuan();
  //     $this->xuqi();
  //     $this->everydayUP();
  //     $this->display();
  // }

  // public function everydayUP(){

  //   $day = date('d',time());

  //   $time = strtotime('today');

  //   $time2 = $time - 86400;
    
  //   $arr['everyday'] = $day - 1;
  //   $arr['datatime'] = date('Y-m',$time2);
  //   $arr['type'] = 1;
  //   $search = M('cash_order_up')->where($arr)->find();
  //   if($search){
  //   }else{
  //     $result = M('cash_order')->where("creat_time > {$time2} AND creat_time < {$time}")->count();
  //     $arr['creat_time'] = time();
  //     $arr['type'] = 1;
  //     $arr['content'] = $result;
  //     $val = M('cash_order_up')->add($arr);
  //   }
  // }


  // public function type(){
  //   $time = $_REQUEST['time3'];
  //   $judgement = $this->judgement($time);
  //   $ym = date('Y-m',$judgement['time']);
  //   for($i=1;$i<$judgement['date'];$i++){
  //     $where['everyday'] = $i;
  //     $where['datatime'] = $ym;
  //     $where['type'] = 1;
  //     $val = M('cash_order_up')->where($where)->field('content')->find()['content'];
  //     $day[] = $i;
  //     $data[] = $val;
  //   }
  //   $this->yqfenxi('type','新增订单',3,$data,$day);
  // }

  // //图形展示时间判断操作
  // public function judgement($time){
  //   if(strpos($time,'-')){
  //       if(strpos($time,date('m',time()))){
  //         $date = date('d',time());
  //         $time = strtotime($time);
  //       }else{
  //         $date = date('t',$time) + 1;
  //         $time = strtotime($time);
  //       }
  //    }else{
  //       $date = date('d',$time);    
  //    }
  //    $arr['date'] = $date;
  //    $arr['time'] = $time;
  //    return $arr;
  // }


  // public function yqfenxi($fd,$ft,$type,$data,$legend) {
  //     import("Org.Util.Chart");
  //     $chart = new \Chart;
  //     $title = $ft; 
  //     $size = 140; 
  //     $width = 550; 
  //     $height = 300;    
  //     ob_end_clean();
  //     if ($type == 1) {
  //       $chart->create3dpie($title,$data,$size,$height,$width,$legend);
  //     }
  //     if ($type == 2) {
  //       $chart->createcolumnar($title,$data,$size,$height,$width,$legend);
  //     }
  //     if ($type == 3) {
  //       $chart->createmonthline($title,$data,$size,$height,$width,$legend);
  //     }
  //     if ($type == 4) {
  //       $chart->createring($title,$data,$size,$height,$width,$legend);
  //     }
  //     if ($type == 5) {
  //       $chart->createhorizoncolumnar($title,$subtitle,$data,$size,$height,$width,$legend);
  //     }
   
  // }

  // public function type2(){
  //   $time = $_REQUEST['time3'];
  //   $judgement = $this->judgement($time);
  //   $ym = date('Y-m',$judgement['time']);
  //   for($i=0;$i<$judgement['date'];$i++){
  //     $where['everyday'] = $i;
  //     $where['datatime'] = $ym;
  //     $where['type'] = 2;
  //     $val = M('cash_order_up')->where($where)->field('content')->find()['content'];
  //     $day[] = $i;
  //     $data[] = $val;
  //   }
  //   $this->yqfenxi('type','回款订单',3,$data,$day);
  // }

  // //统计每天回款订单
  // public function huikuan(){
  //   //获取当前的天数
  //   $data = date('d',time());

  //   //获取当前0点的时间戳
  //   $time = strtotime('today');

  //   //获取前一天0点的时间戳
  //   $time2 = $time - 86400;
  //   $arr['everyday'] = $data - 1;
  //   $arr['datatime'] = date('Y-m',$time2);
  //   $arr['type'] = 2;
  //   $search = M('cash_order_up')->where($arr)->find();
  //   if($search){

  //   }else{
  //     //获取该时间段中的所有订单
  //     $result = M('cash_bf_dk')->where("k_time > {$time2} AND k_time < {$time} AND status = 'S' AND trans_type <> 3")->count();

  //     $arr['creat_time'] = time();
  //     $arr['type'] = 2;
  //     $arr['content'] = $result;
  //     $val = M('cash_order_up')->add($arr);
  // }
  // } 

  //  public function type3(){
  //   $time = $_REQUEST['time3'];
  //   $judgement = $this->judgement($time);
  //   $ym = date('Y-m',$judgement['time']);
  //   for($i=0;$i<$judgement['date'];$i++){
  //     $where['everyday'] = $i;
  //     $where['datatime'] = $ym;
  //     $where['type'] = 3;
  //     $val = M('cash_order_up')->where($where)->field('content')->find()['content'];
  //     $day[] = $i;
  //     $data[] = $val;
  //   }
  //   $this->yqfenxi('type','续期订单',3,$data,$day);
  // }

  // //统计每天续期的订单
  // public function xuqi(){
  //   //获取当前的天数
  //   $data = date('d',time());

  //   //获取当前0点的时间戳 
  //   $time = strtotime('today');

  //   //计算前一天0点的时间戳
  //   $time2 = $time - 86400;

  //   $arr['everyday'] = $data - 1;
  //   $arr['datatime'] = date('Y-m',$time2);
  //   $arr['type'] = 3;
  //   $search = M('cash_order_up')->where($arr)->find();
  //   if($search){

  //   }else{
  //     $result = M('cash_bf_dk')->where("k_time > {$time2} AND k_time < {$time} AND status = 'S' AND trans_type = 3")->count();
  //     $arr['creat_time'] = time();
  //     $arr['type'] = 3;
  //     $arr['content'] = $result;
  //     $val = M('cash_order_up')->add($arr);
  //   }
  // }


  public function index(){
    $search = $this->search($_REQUEST);
    //从订单表中获取订单增长量
    $result = M()->query("select count(*) as num, FROM_UNIXTIME(creat_time,'%".$search['genre']."') as time from cash_order where ".$search['where']." group by time");
    //获取当前条件下的回款订单量
    $val = M()->query("select count(*) as num,FROM_UNIXTIME(k_time,'%".$search['genre']."') as time from cash_bf_dk where ".$search['where1']." AND status = 'S' AND trans_type <> 3 "." group by time");

    $val2 = M()->query("select count(*) as num,FROM_UNIXTIME(k_time,'%".$search['genre']."') as time from cash_bf_dk where ".$search['where1']." AND status = 'S' AND trans_type = 3 "." group by time");

    //每天放款订单统计
    $val3 = M()->query("select count(*) as num,FROM_UNIXTIME(loan_time,'%".$search['genre']."') as time from cash_order where ".$search['where2']." group by time");
    $orderUP = ['time'=>array_column($result,'time'),'num'=>array_column($result,'num')];
    //var_dump($orderUP);exit;
    $huikuan = ['time'=>array_column($val,'time'),'num'=>array_column($val,'num')];
    $xuqi = ['time'=>array_column($val2,'time'),'num'=>array_column($val2,'num')];
    $fk = ['time'=>array_column($val3,'time'),'num'=>array_column($val3,'num')];
    $this->assign('orderUP',json_encode($orderUP));  //订单增长量
    $this->assign('huikuan',json_encode($huikuan));   //回款订单
    $this->assign('xuqi',json_encode($xuqi));    //续期订单
    $this->assign('fk',json_encode($fk));    //放款订单
    $this->display();
  }

  //查询删选条件
  public function search($val){
    $time = isset($_REQUEST['time6']) && !empty($_REQUEST['time6']) ? strtotime($_REQUEST['time6']) : time();
    //获取本月第一天
    $firstday = date('Y-m-01',$time);
    $genre = isset($_REQUEST['genre']) ? $_REQUEST['genre'] : 'm';
    switch ($genre) {
      case 'm':
        $title = date('Y').'月订单增长量';
        $where = 'creat_time BETWEEN '.strtotime(date('Y-01-01',$time)).' AND '.strtotime(date('Y-12-31',$time));
        $where1 = 'k_time BETWEEN '.strtotime(date('Y-01-01',$time)).' AND '.strtotime(date('Y-12-31',$time));
        $where2 = 'loan_time BETWEEN '.strtotime(date('Y-01-01',$time)).' AND '.strtotime(date('Y-12-31',$time));
        break;
      case 'd':
        $title = date('Y-m',$time).'日订单增长量';
        $where = 'creat_time BETWEEN '.strtotime($firstday).' AND '.strtotime(date('Y-m-d',strtotime("$firstday +1 month")));
        $where1 = 'k_time BETWEEN '.strtotime($firstday).' AND '.strtotime(date('Y-m-d',strtotime("$firstday +1 month")));
        $where2 = 'loan_time BETWEEN '.strtotime($firstday).' AND '.strtotime(date('Y-m-d',strtotime("$firstday +1 month")));
        break;
      case 'H':
        $title = date('Y-m-d',$time).'每小时订单增长量';
        $where = 'creat_time BETWEEN '.strtotime(date('Y-m-d 0:0:0',$time)).' AND '.strtotime(date('Y-m-d 23:59:59',$time)); 
        $where1 = 'k_time BETWEEN '.strtotime(date('Y-m-d 0:0:0',$time)).' AND '.strtotime(date('Y-m-d 23:59:59',$time)); 
        $where2 = 'loan_time BETWEEN '.strtotime(date('Y-m-d 0:0:0',$time)).' AND '.strtotime(date('Y-m-d 23:59:59',$time));
        break;
      default:
        # code...
        break;
    }
    $this->assign('time',$time);
    $this->assign('genre',$genre);
    $this->assign('title',$title);
    $date['genre'] = $genre;
    $date['where'] = $where;
    $date['where1'] = $where1;
    $date['where2'] = $where2;
    return $date;
  }


    public function type(){
    import("Org.Util.Chart");
      $chart = new \Chart;
     // $type = $_REQUEST['type'];
      $title= $_REQUEST['title'];
      $set  = json_decode($_REQUEST['orderUP'],true);
      $size  =  0; 
      $width = 1300; 
      $height = 600;
      $data   = $set['num']; 
      $legend = $set['time'];
      ob_end_clean();
      $chart->createmonthline($title,$data,$size,$height,$width,$legend);
  }

   public function type2(){
    import("Org.Util.Chart");
      $chart = new \Chart;
     // $type = $_REQUEST['type'];
      $title= $_REQUEST['title'];
      $set  = json_decode($_REQUEST['huikuan'],true);
      $size  =  0; 
      $width = 1300; 
      $height = 600;
      $data   = $set['num']; 
      $legend = $set['time'];
      ob_end_clean();
      $chart->createmonthline($title,$data,$size,$height,$width,$legend);
  }

   public function type3(){
    import("Org.Util.Chart");
      $chart = new \Chart;
     // $type = $_REQUEST['type'];
      $title= $_REQUEST['title'];
      $set  = json_decode($_REQUEST['xuqi'],true);
      $size  =  0; 
      $width = 1300; 
      $height = 600;
      $data   = $set['num']; 
      $legend = $set['time'];
      ob_end_clean();
      $chart->createmonthline($title,$data,$size,$height,$width,$legend);
  }
  

  public function type4(){
    import("Org.Util.Chart");
      $chart = new \Chart;
     // $type = $_REQUEST['type'];
      $title= $_REQUEST['title'];
      $set  = json_decode($_REQUEST['fk'],true);
      $size  =  0; 
      $width = 1300; 
      $height = 600;
      $data   = $set['num']; 
      $legend = $set['time'];
      ob_end_clean();
      $chart->createmonthline($title,$data,$size,$height,$width,$legend);
  }

}